import promptAction from '@ohos.promptAction'
import picker from '@ohos.file.picker';
import JsDataStore from './JsDataStore'
import uri from '@ohos.uri';
import fs from '@ohos.file.fs';
import JsLogger from './JsLogger'

export class JsDialog {

    constructor() {
    }

    messageBox(handler : number, title : string, text : string, buttons : Array<string>): boolean {
        var opt: promptAction.ShowDialogOptions = {
            title: title,
            message: text,
            buttons: [{
                text: "OK",
                color: "#000000"
            }]
        };
        if (buttons != null && buttons.length > 0) {
            var first : promptAction.Button = {
                text: buttons[0],
                color: '#000000',
            }
            opt.buttons[0] = first;
            for (var i = 1; i < buttons.length; i++) {
                var button : promptAction.Button = {
                    text: buttons[i],
                    color: '#000000',
                }
                opt.buttons.push(button)
            }
        }

        promptAction.showDialog(opt, (err, data) => {
            if (err) {
                JsLogger.error("show dialog error: %{public}s", JSON.stringify(err));
            }
            let index = err ? -1 : data.index;
            JsDataStore.getQtNativeModule("QPA").dialogResult(handler, index);
        });
        return true;
    }

    isVideo(filter: string): boolean {
        return filter.includes("mp4") || filter.includes("MPEG")
        || filter.includes("MPG") || filter.includes("DAT")
        || filter.includes("MOV") || filter.includes("FLV");
    }

    isAudio(filter: string): boolean {
        return filter.includes("mp3") || filter.includes("wma") || filter.includes("ogg") || filter.includes("flac")
        || filter.includes("wv");
    }

    isImage(filter: string): boolean {
        return filter.includes("png") || filter.includes("jpeg")
        || filter.includes("jpg") || filter.includes("bmp")
    }

    async openFileDialog(handler: number, filter: string) : Promise<boolean> {
        // let isImage = this.isImage(filter);
        // let isVideo = this.isVideo(filter);
        // let isAudio = this.isAudio(filter);
        // if (isImage || isVideo) {
        //     const photoSelectOptions = new picker.PhotoSelectOptions();
        //     if (isImage)
        //         photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_TYPE;
        //     if (isVideo)
        //         photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.VIDEO_TYPE;
        //     if (isVideo && isImage)
        //         photoSelectOptions.MIMEType = picker.PhotoViewMIMETypes.IMAGE_VIDEO_TYPE;
        //     photoSelectOptions.maxSelectNumber = 5;
        //     const photoViewPicker = new picker.PhotoViewPicker();
        //     photoViewPicker.select(photoSelectOptions).then((photoSelectResult) => {
        //         JsLogger.info('photoViewPicker.select to file succeed and uri is: %{public}', uri);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, photoSelectResult.photoUris);
        //     }).catch((err) => {
        //         JsLogger.error(`Invoke photoViewPicker.select failed, code is %{public}d, message is %{public}s`, err.code, err.message);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        //     })
        // } else if (isAudio) {
        //     const audioSelectOptions = new picker.AudioSelectOptions();
        //     const audioViewPicker = new picker.AudioViewPicker();
        //     audioViewPicker.select(audioSelectOptions).then(audioSelectResult => {
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, audioSelectResult);
        //         JsLogger.info('audioViewPicker.select to file succeed and uri is: %{public}s', JSON.stringify(audioSelectResult));
        //     }).catch((err) => {
        //         JsLogger.error(`Invoke audioViewPicker.select failed, code is %{public}d, message is %{public}s`, err.code, err.message);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        //     })
        // } else  {
        //     const documentSelectOptions = new picker.DocumentSelectOptions();
        //     const documentViewPicker = new picker.DocumentViewPicker();
        //     documentViewPicker.select(documentSelectOptions).then((documentResult) => {
        //         JsLogger.info('documentViewPicker.select to file succeed and uri is: %{public}s', JSON.stringify(documentResult));
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, documentResult);
        //     }).catch((error) => {
        //         JsLogger.error(`Invoke documentViewPicker.select failed, code is %{public}d, message is %{public}s`, error.code, error.message);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        //     });
        // }

        let config = {
            action: 'ohos.want.action.OPEN_FILE',
            parameters: {
                startMode: 'choose',
            }
        }

        let context = JsDataStore.getContext();

        context.startAbilityForResult(config).then((result) => {
            // 获取到文档文件的uri
            let select_item_list = result.want.parameters.select_item_list;
            JsLogger.info("select file: %{public}s", select_item_list.toString());
            // 有的系统是file://xxx/xxx/xx/xx
            JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, [select_item_list.toString()]);
        }).catch((error) => {
            JsLogger.error("open file dialog result %{public}s", JSON.stringify(error));
            JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        });
        return true;
    }

    async saveFileDialog(handler : number, fileName: string) : Promise<boolean> {
        // let isImage = this.isImage(fileName);
        // let isVideo = this.isVideo(fileName);
        // let isAudio = this.isAudio(fileName);
        // if (isImage || isVideo) {
        //     const photoSaveOptions = new picker.PhotoSaveOptions();
        //     photoSaveOptions.newFileNames = [fileName];
        //     const photoViewPicker = new picker.PhotoViewPicker();
        //     photoViewPicker.save(photoSaveOptions).then((photoResult) => {
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, photoResult);
        //     }).catch((error) => {
        //         JsLogger.error(`Invoke documentViewPicker.select failed, code is %{public}d, message is %{public}s`, error.code, error.message);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        //     });
        // } else if (isAudio) {
        //     const audioSaveOptions = new picker.AudioSaveOptions();
        //     audioSaveOptions.newFileNames = [fileName];
        //     const audioViewPicker = new picker.AudioViewPicker();
        //     audioViewPicker.save(audioSaveOptions).then((audioResult) => {
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, audioResult);
        //     }).catch((error) => {
        //         JsLogger.error(`Invoke documentViewPicker.select failed, code is %{public}d, message is %{public}s`, error.code, error.message);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        //     });
        // } else {
        //     const documentSaveOptions = new picker.DocumentSaveOptions();
        //     documentSaveOptions.newFileNames = [fileName];
        //     const documentViewPicker = new picker.DocumentViewPicker();
        //     documentViewPicker.save(documentSaveOptions).then((documentResult) => {
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, documentResult);
        //     }).catch((error) => {
        //         JsLogger.error(`Invoke documentViewPicker.select failed, code is %{public}d, message is %{public}s`, error.code, error.message);
        //         JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        //     });
        // }
        // return true;
        let config = {
            action: 'ohos.want.action.CREATE_FILE',
            parameters: {
                startMode: 'save',
                key_pick_file_name: [fileName],
                saveFile: fileName,
            }
        }

        try {
            let context = JsDataStore.getContext()
            let result = await context.startAbilityForResult(config);
            JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, [result.want.parameters.pick_path_return.toString()]);
        } catch (error) {
            JsLogger.info("startAbilityForResult Promise.Reject is called, error.code = %{public}s",  error.code)
            JsDataStore.getQtNativeModule("QPA").selectedFilesResult(handler, []);
        }
        return true;
    }
}
